package com.kai.steering.data.service.ctl;

import com.kai.steering.data.service.entity.domainObject.CaretakerDO;
import com.kai.steering.data.service.entity.dto.CaretakerQueryDTO;
import com.kai.steering.data.service.entity.po.Caretaker;
import com.kai.steering.data.service.entity.vo.CaretakerVO;
import com.kai.steering.data.service.service.CaretakerService;
import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.ctl.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import javax.validation.Valid;

import com.kai.steering.common.vo.Page;
import com.kai.steering.common.web.vo.ReturnCodeExt;

import java.util.List;


/**
 * <pre>
 * 管理员 前端控制器
 * </pre>
 *
 * @author kai
 * @since 2022-02-23
 */
@Slf4j
@RestController
@RequestMapping("/caretaker")
@Api(tags = "管理员 API")
public class CaretakerController extends BaseController {
    @Autowired
    private CaretakerService caretakerService;


    @PostMapping
    @ApiOperation(value = "管理员-添加")
    public Result<Boolean> addCaretaker(@Valid @RequestBody CaretakerQueryDTO caretakerQueryDTO) {

        /**
         * 参数检查
         */
        if (StringUtils.isEmpty(caretakerQueryDTO.getCaretakerId()) || StringUtils.isEmpty(caretakerQueryDTO.getCaretakerName()) ||
                StringUtils.isEmpty(caretakerQueryDTO.getPassword())) {
            return Result.failed("参数不完整");
        }

        /**
         * 构建新对象
         */
        CaretakerDO newCaretakerDO = new CaretakerDO(caretakerQueryDTO);

        /**
         * 调用服务
         */
        boolean flag = caretakerService.saveCaretaker(newCaretakerDO);
        return Result.ok(flag);
    }


    @PutMapping("/{id}")
    @ApiOperation(value = "管理员-编辑")
    public Result<Boolean> updateCaretaker(@PathVariable("id") Serializable id, @Valid @RequestBody CaretakerQueryDTO caretakerQueryDTO) {

        /**
         * 参数检查
         */
        //id
        if (StringUtils.isEmpty(id.toString())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象[" + id + "]为空");
        }
        //其他
        if (StringUtils.isEmpty(caretakerQueryDTO.getCaretakerName())|| StringUtils.isEmpty(caretakerQueryDTO.getPassword())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("属性为空");
        }
        /**
         * 构建业务对象
         */
        CaretakerDO caretakerDO = new CaretakerDO(caretakerQueryDTO);
        caretakerDO.setCaretakerId(id.toString());

        //修改属性
        boolean flag = caretakerService.updateCaretaker(id,caretakerDO);
        return Result.ok(flag);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "管理员-删除")
    public Result<Boolean> deleteCaretaker(@PathVariable("id") Long id) {

        boolean flag = caretakerService.deleteCaretaker(id);
        return Result.ok(flag);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "管理员-详情")
    public Result<CaretakerVO> getCaretaker(@PathVariable("id") Serializable id) {
        CaretakerVO caretakerQueryVo =new CaretakerVO( caretakerService.getCaretakerById(id));
        if (caretakerQueryVo == null) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("id[" + id + "]对象不存在");
        }

        return Result.ok(caretakerQueryVo);
    }


    @PostMapping("/list")
    @ApiOperation(value = "管理员-综合查询")
    public Result<Page<CaretakerVO>> getCaretakerPageList(@Valid @RequestBody CaretakerQueryDTO caretakerQueryDTO) {
        Page<CaretakerVO> paging = caretakerService.getCaretakerPageList(caretakerQueryDTO);
        return Result.ok(paging);
    }


//    @GetMapping("/list")
//    @ApiOperation(value = "管理员-全部列表")
//    public Result<List<CaretakerVO>> getCaretakerAllList() {
//
//        List<CaretakerVO> list = null;
//        return Result.ok(list);
//    }


}